<?php
/**
 * Created by PhpStorm.
 * User: CY
 * Date: 2018/3/6
 * Time: 17:45
 */

namespace app\api\controller\v1;

use app\api\validate\IDCollection;
use app\api\validate\IDMustBePositiveInt;
use app\api\validate\ThemeProduct;
use app\lib\exception\SuccessMessage;
use app\lib\exception\ThemeException;
use think\Controller;
use app\api\model\Theme as ThemeModel;

/**
 * 主题推荐，主题指首页里多个聚合在一起的商品，注意同专题区分
 * Class Theme
 * @package app\api\controller\v1
 */
class Theme extends Controller
{
    /**
     * 获得主题列表
     * @url /theme?ids=id1,id2,id3...
     * @param string $ids
     * @return false|\PDOStatement|string|\think\Collection
     * @throws ThemeException
     */
    public function getSimpleList($ids = ''){
        $validate = new IDCollection();
        $validate->goCheck();

        $ids = explode(',',$ids);
        $result = ThemeModel::with('topicImg,headImg')->select($ids);
        if($result->isEmpty()){
            throw new ThemeException();
        }
        return $result;
    }

    /**
     * 获得具体主题和其对应的所有商品信息
     * @url /theme/:id
     * @param $id
     * @return array|false|\PDOStatement|string|\think\Model
     * @throws ThemeException
     */
    public function getComplexOne($id){
        $validate = new IDMustBePositiveInt();
        $validate->goCheck();

        $theme = ThemeModel::getThemeWithProducts($id);
        if(!$theme){
            throw new ThemeException();
        }
        return $theme->hidden(['products.summary']);
    }

    /**
     * 新增主题下的商品
     * @url /theme/:t_id/product/:p_id
     * @Http POST
     * @param $t_id
     * @param $p_id
     * @return SuccessMessage
     */
    public function addThemeProduct($t_id,$p_id){
        $validate = new ThemeProduct();
        $validate->goCheck();
        ThemeModel::addThemeProduct($t_id,$p_id);
        return new SuccessMessage();
    }

    /**
     * 删除指定主题下的商品
     * @url /theme/:t_id/product/:p_id
     * @Http DELETE
     * @param $t_id
     * @param $p_id
     * @return SuccessMessage
     */
    public function deleteThemeProduct($t_id,$p_id){
        $validate = new ThemeProduct();
        $validate->goCheck();
        $themeID = (int)$t_id;
        $productID = (int)$p_id;
        ThemeModel::deleteThemeProduct($themeID,$productID);
        return new SuccessMessage([
            'code' => 204
        ]);
    }














}